package com.synergy.stockflow.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.production.entity.InboundOrder;
import com.synergy.stockflow.production.entity.InboundOrderDetail;
import com.synergy.stockflow.production.entity.ProductionPlan;
import com.synergy.stockflow.production.entity.ProductionPlanDetail;
import com.synergy.stockflow.production.service.InboundOrderDetailService;
import com.synergy.stockflow.production.service.InboundOrderService;
import com.synergy.stockflow.production.service.ProductionPlanDetailService;
import com.synergy.stockflow.production.service.ProductionPlanService;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventoryTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/inboundOrder")
public class InboundOrderController {

    @Autowired
    private InboundOrderService inboundOrderService;

    @Autowired
    private InboundOrderDetailService inboundOrderDetailService;

    @Autowired
    private InventoryTransactionService inventoryTransactionService;

    @Autowired
    private ProductionPlanDetailService productionPlanDetailService;


    @GetMapping("/page")
    public Result<Page<InboundOrder>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                       @RequestParam(name = "size", defaultValue = "2") int size,
                       @RequestParam(name = "inboundCode", required = false) String inboundCode,
                       @RequestParam(name = "inboundName", required = false) String inboundName,
                       @RequestParam(name = "planId", required = false) String planId,
                       @RequestParam(name = "warehouseId", required = false) String warehouseId) {
        QueryWrapper<InboundOrder> queryWrapper = new QueryWrapper<>();
        if(inboundCode != null){
            queryWrapper.lambda().like(InboundOrder::getInboundCode,inboundCode);
        }
        if(inboundName != null){
            queryWrapper.lambda().like(InboundOrder::getInboundName,inboundName);
        }
        if(planId != null){
            queryWrapper.lambda().like(InboundOrder::getPlanId,planId);
        }
        if(warehouseId != null){
            queryWrapper.lambda().eq(InboundOrder::getWarehouseId,warehouseId);
        }
        Page<InboundOrder> objectPage = new Page<>(current, size);
        inboundOrderService.page(objectPage, queryWrapper);
        return Result.success(objectPage);
    }

    @GetMapping("/detail")
    public Result<List<InboundOrderDetail>> detail(@RequestParam String inboundId)  {
        List<InboundOrderDetail> inboundOrderDetails = inboundOrderDetailService.selectByInboundId(inboundId);
        return Result.success(inboundOrderDetails);
    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody InboundOrder inboundOrder) {
        inboundOrderService.updateById(inboundOrder);
        return Result.success(inboundOrder);
    }

    @PutMapping("/updateDetail")
    public Result<?> updateDetail(@RequestBody List<InboundOrderDetail> inboundOrderDetails) {
        inboundOrderDetailService.updateBatchById(inboundOrderDetails);
        return Result.success(inboundOrderDetails);
    }

    @PostMapping("/save")
    public Result save(@RequestBody InboundOrder inboundOrder) {
        inboundOrder.setUpdateTime(new Date());
        inboundOrder.setCheckTime(new Date());
        return Result.success(inboundOrderService.save(inboundOrder));
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam String inboundId) {
        return Result.success(inboundOrderService.removeById(inboundId));
    }

    @DeleteMapping("/batch")
public Result batchDelete(@RequestBody List<String> inboundIds) {
        return Result.success(inboundOrderService.removeByIds(inboundIds));
    }

    @PostMapping("/execute")
    public Result exportExcel(@RequestBody InboundOrder inboundOrder) {
        InventoryTransaction inventoryTransaction = new InventoryTransaction();
        inventoryTransaction.setTransactionType("PO_IN");
        inventoryTransaction.setSourceDocId(inboundOrder.getInboundId());
        inventoryTransaction.setSourceDocType("inbound_order");
        inventoryTransaction.setWarehouseId(inboundOrder.getWarehouseId());
        inventoryTransaction.setMaterialId(inboundOrder.getProductId());
        inventoryTransaction.setOperatorId(inboundOrder.getOperatorId());
        inventoryTransaction.setBatchNo(inboundOrder.getBatchNo());
        inventoryTransaction.setQuantity(inboundOrder.getActualQuantity());
        inventoryTransaction.setTransactionTime(new Date());
        inventoryTransaction.setRemarks("成品入库"+inboundOrder.getInboundName());

        //修改入库状态
        inboundOrder.setInboundStatus(1);
        inboundOrderService.updateById(inboundOrder);

        //修改生产计划中的已生产数量
        productionPlanDetailService.updateByPlanId(inboundOrder.getActualQuantity(),inboundOrder.getActualQuantity(),inboundOrder.getPlanId());

        return Result.success(inventoryTransactionService.save(inventoryTransaction));
    }
}
